package org.xq.softcup.mq.remoting.net.impl.netty.client;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xq.softcup.mq.remoting.invoker.XQRpcInvokerFactory;
import org.xq.softcup.mq.remoting.net.params.XQRpcResponse;
@Slf4j
public class NettyClientHandler extends SimpleChannelInboundHandler<XQRpcResponse> {
    private XQRpcInvokerFactory xqRpcInvokerFactory;

    public NettyClientHandler(XQRpcInvokerFactory xqRpcInvokerFactory) {
        this.xqRpcInvokerFactory = xqRpcInvokerFactory;
    }

    protected void channelRead0(ChannelHandlerContext ctx, XQRpcResponse xqRpcResponse) throws Exception {
        this.xqRpcInvokerFactory.notifyInvokerFuture(xqRpcResponse.getRequestId(), xqRpcResponse);
    }

    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.error("Netty客户端连接异常", cause);
        ctx.close();
    }
}
